package com.qf.joint.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qf.joint.pojo.Recruiter;
import com.qf.joint.pojo.Recruitpost;
import com.qf.joint.pojo.Tradepost;
import com.qf.joint.service.RecruitpostService;
import com.qf.joint.utils.Info;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@Controller
@RequestMapping("/recruitpost")
@CrossOrigin
public class RecruitpostController {
    @Autowired
    private RecruitpostService recruitpostService;
    //职位类型和关键字查询
    @RequestMapping(value = "/findcon")
    @ResponseBody
    public List<Recruitpost> rpkey(String type,String key){
        try {
          type=new String(type.getBytes("ISO-8859-1"),"UTF-8");
            key=new String(key.getBytes("ISO-8859-1"),"UTF-8");
            System.out.println("type="+type+";key="+key);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        Recruitpost r=new Recruitpost();
        r.setPosttype(type);
        r.setRpkey(key);
        List<Recruitpost> list= recruitpostService.selectAll(r);
        /*Info<Recruitpost> info=new Info<>();
        info.setData(list);
        info.setMsg("没有查到数据");
        info.setCode(0);
        info.setCount(list.size());*/
        return list;
    }
    //地点查询
    @RequestMapping(value = "/details")
    @ResponseBody
    public Info<Recruitpost> details(String details){
        try {
            details=new String(details.getBytes("ISO-8859-1"),"UTF-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        Recruitpost r=new Recruitpost();
        r.setDetails(details);
        List<Recruitpost> list= recruitpostService.select(r);
        Info<Recruitpost> info=new Info<>();
        info.setData(list);
        info.setMsg("没有查到数据");
        info.setCode(1);
        info.setCount(list.size());
        return info;
    }
    //条件查询
    @RequestMapping(value = "/findlike")
    @ResponseBody
    public List<Recruitpost> post(String condition){
        try {
            condition=new String(condition.getBytes("ISO-8859-1"),"UTF-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        Recruitpost r=new Recruitpost(null,null,condition,condition,condition,condition,condition,condition,condition,null,null);
        List<Recruitpost> list= recruitpostService.selectAllmore(r);
        /*Info<Recruitpost> info=new Info<>();
        info.setData(list);
        info.setMsg("没有查到数据");
        info.setCode(1);
        info.setCount(list.size());*/
        return list;
    }
    //联合查询
    @RequestMapping(value = "/show",method = RequestMethod.GET)
    @ResponseBody
    public List<Recruitpost> select1(){
        Recruitpost r=new Recruitpost();
        r.setPosttype("全职");
        r.setRpkey("Java");
        List<Recruitpost> list= recruitpostService.selectAll(r);
        /*System.out.println(list);
        Info<Recruitpost> info=new Info<>();
        info.setData(list);
        info.setMsg("没有查到数据");
        info.setCode(0);
        info.setCount(list.size());*/
        return list;
    }
    //联合查询一个
    @RequestMapping(value = "/findid/{rpid}",method = RequestMethod.GET)
    @ResponseBody
    public List<Recruitpost> select1(@PathVariable("rpid")int rpid){
        Recruitpost r=new Recruitpost();
        r.setRpid(rpid);
        List<Recruitpost> list= recruitpostService.selectAll(r);
        /*System.out.println(list);
        Info<Recruitpost> info=new Info<>();
        info.setData(list);
        info.setMsg("没有查到数据");
        info.setCode(0);
        info.setCount(list.size());*/
        return list;
    }


    @ResponseBody
    @RequestMapping(value = "/test",method = RequestMethod.POST)
    public Info insert(Recruitpost recruitpost){
        int k=recruitpostService.insert(recruitpost);
        Info info=new Info<>();
        info.setMsg("新增成功");
        info.setCode(k);
        return info;
    }
    @RequestMapping(value = "/insert",method = RequestMethod.POST)
    public String insertNew(Recruitpost recruitpost){
        int k=recruitpostService.insert(recruitpost);
        return "redirect:/admin/admin";
    }
    @ResponseBody
    @RequestMapping(value = "/test",method = RequestMethod.PUT)
    public Info update(Recruitpost recruitpost){
        System.out.println(recruitpost);
        int k=recruitpostService.update(recruitpost);
        Info info=new Info<>();
        info.setMsg("修改成功");
        info.setCode(k);
        return info;
    }

    @RequestMapping(value = "/update",method = RequestMethod.GET)
    public String updateNew(Recruitpost recruitpost){
        int k=recruitpostService.update(recruitpost);
        return "redirect:/admin/admin";
    }

    @ResponseBody
    @RequestMapping(value = "/test")
    public Info<Recruitpost> select(@RequestParam(defaultValue = "5") Integer page,
                                    @RequestParam(defaultValue = "5") Integer limit){
        PageHelper.startPage(page,limit);
        Recruitpost recruitpost=new Recruitpost();
        List<Recruitpost> list=recruitpostService.select(recruitpost);
        PageInfo<Recruitpost> pageInfo =new PageInfo<>(list);
        Info<Recruitpost> info=new Info<Recruitpost>();
        if (list!=null&list.size()>0){
            info.setCode(0);
            info.setCount((int) pageInfo.getTotal());
            info.setData(pageInfo.getList());
           // info.setCount(list.size());
            System.out.println(pageInfo.getTotal());
        }
        info.setMsg("没有查到数据");
        return info;
    }
    @ResponseBody
    @RequestMapping(value = "/test",method = RequestMethod.DELETE)
    public Info delete(Integer[] rpid){
        System.out.println(Arrays.toString(rpid));
        List<Integer> list=new ArrayList<>();
        for(int i=0;i<rpid.length;i++){
            list.add(rpid[i]);
        }
        int k=recruitpostService.delete(list);
        Info info=new Info<>();
        info.setMsg("删除成功");
        info.setCode(k);
        return info;
    }

    @RequestMapping(value = "/delete",method = RequestMethod.GET)
    public String deleteNew(String idArry){
        String[] rpid = idArry.split(",");
        System.out.println(Arrays.toString(rpid));
        List<Integer> list=new ArrayList<>();
        for(int i=0;i<rpid.length;i++){
            list.add(Integer.valueOf(rpid[i]));
        }
        int k=recruitpostService.delete(list);
        return "redirect:/admin/admin";
    }
}
